<template>
    <h2 style="text-align: center;">科室信息添加</h2>
    <table border="1" class="table table-bordered">
        <tbody>
            <tr>
                <th>科室名称</th>
                <th>
                    <input type="text" placeholder="请输入科室名称" v-model="Info.Dept_Name" />
                </th>
            </tr>

            <tr>
                <th>科室人数</th>
                <th>
                    <input type="text" placeholder="请输入科室人数"  v-model="Info.Dept_Num"/>
                </th>
            </tr>

            <tr>
                <th>科室介绍</th>
                <th>
                    <textarea cols="20" rows="4" placeholder="请输入科室介绍" v-model="Info.Dept_Info"></textarea>
                </th>
            </tr>

            <tr>
                <th>添加人</th>
                <th>
                    <input type="text" placeholder="请输入添加人" v-model="Info.Member_Name"/>
                </th>
            </tr>

            <tr>
                <th colspan="2" style="text-align: center;">
                    <input type="button" value="添加科室信息" class="btn btn-success" @click="AddDept" style="margin-right: 10px;"/>
                    <a href="/ShowDept">返回列表</a>
                </th>
            </tr>
        </tbody>
    </table>
</template>



<script setup lang="ts">
    import {ref} from 'vue';
    import axios from 'axios';
    import { useRouter } from 'vue-router';
    import { useCounterStore } from '@/stores/counter';


    const router=useRouter();
    const store=useCounterStore();
    if(store.Userid=="")
    {
        alert('请选登录');
        router.push("/Login");
    }
    //添加响应式数据
    const Info=ref({
        Dept_Name: "",
        Dept_Num: "",
        Dept_Info: "",
        Dept_IsActive: true,
        Member_Name: store.UserName
    })

    //添加
    const AddDept=()=>{
        if(Info.value.Dept_Name=="")
        {
            alert('科室名称不能为空');
            return;
        }
        if(Info.value.Dept_Num=="")
        {
            alert('科室人数不能为空');
            return;
        }
        if(Info.value.Dept_Info=="")
        {
            alert('科室介绍不能为空');
            return;
        }
        axios.post("https://localhost:44306/api/Department/AddDept",Info.value)
        .then(res=>{
            if(res.data==-1)
            {
                alert('科室名称已存在，请重新输入');
                return;
            }
            else if(res.data>0)
            {
                alert('科室信息添加成功');
                router.push("/ShowDept");//跳转到显示页面
            }
            else
            {
                alert('科室信息添加失败');
            }
        })
    }
</script>